package com.doctor.windflower_doctor.fragment;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.MessageAction;
import com.doctor.windflower_doctor.adapter.MessageOneAdapter;
import com.doctor.windflower_doctor.adapter.MyQuestionAdapter;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.MessageBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.doctor.windflower_doctor.view.PullToCustomListView;
import com.doctor.windflower_doctor.view.PullToCustomListView.OnAutoLoadBottomListener;
import com.doctor.windflower_doctor.view.PullToRefreshBase.OnRefreshListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MessageOne extends BaseFragment {
	private PullToCustomListView cuto_listView;
	private ListView lv_message;
	private MessageOneAdapter adapter;
	private String message_id;
	private RelativeLayout layout;
	private LoadingDialog loadingDialog;
	private TextView tv_answer,tv_message,tv_question;
	private String requestUrl = Constent.URL+"doctor/getMessages?doctorId="
			+ SharedPreferencesDB.getInstance(getActivity()).getDocotorId() + "&message=0&size=10&type=1";

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater
				.inflate(R.layout.fragment_message_one, container, false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		adapter = new MessageOneAdapter(getActivity());
		lv_message.setAdapter(adapter);
		lv_message.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}
		});
		cuto_listView.setOnAutoLoadBottomListener(new OnAutoLoadBottomListener() {

			@Override
			public void onBottomLoad() {
				
				getLoadUser();
				
			}
		});
		cuto_listView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				adapter.clear();
				getUser(Constent.URL+"doctor/getMessages?doctorId="
						+ SharedPreferencesDB.getInstance(getActivity()).getDocotorId() + "&message=0&size=10&type=1");
				cuto_listView.onRefreshDownComplete();

			}
		});
	}

	@Override
	protected void findViews(View view) {
		// TODO Auto-generated method stub
		cuto_listView = (PullToCustomListView) view
				.findViewById(R.id.lv_message);
		lv_message = cuto_listView.getRefreshableView();
		tv_answer=(TextView) view.findViewById(R.id.tv_answer);
		tv_answer.setVisibility(View.GONE);
		tv_message=(TextView) view.findViewById(R.id.textView1);
		tv_question=(TextView) view.findViewById(R.id.textView2);
		tv_message.setText("暂时没有新的消息，");
		tv_question.setText("稍后再过来看看吧");
		loadingDialog = new LoadingDialog(getActivity());
		layout = (RelativeLayout) view.findViewById(R.id.question_bar);
		getUser(requestUrl);
	}

	private void getUser(String requestUrl) {
		HttpMethod requestMethod = HttpMethod.GET;
		HttpRequestCallBack<MessageAction> requestCallBack = new HttpRequestCallBack<MessageAction>(
				new JsonParser(), MessageAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<MessageAction> responseInfo) {
				// TODO Auto-generated method stub
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						List<MessageBeen> been=responseInfo.success.data.messages;
						adapter.addData(been);
						adapter.notifyDataSetChanged();
						if(adapter.getCount()>0){
							layout.setVisibility(View.GONE);
							message_id=((MessageBeen)(adapter.getItem(adapter.getCount()-1)))._id;
						}
						else {
							layout.setVisibility(View.VISIBLE);
						}
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub
				layout.setVisibility(View.VISIBLE);
			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				requestUrl, requestCallBack);
		HttpRequest.send(params);
	}
	private void getLoadUser(){
		HttpMethod requestMethod = HttpMethod.GET;
		requestUrl=Constent.URL+"doctor/getMessages?doctorId="
				+ SharedPreferencesDB.getInstance(getActivity()).getDocotorId() + "&message=0&size=10&type=0&id="+message_id;
		HttpRequestCallBack<MessageAction> requestCallBack = new HttpRequestCallBack<MessageAction>(
				new JsonParser(), MessageAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<MessageAction> responseInfo) {
				// TODO Auto-generated method stub
				loadingDialog.close();
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						List<MessageBeen> been=responseInfo.success.data.messages;
						cuto_listView.onLoadBottomComplete();
						if (been == null) {
							Toast.makeText(getActivity(), "网络请求失败",
									Toast.LENGTH_SHORT).show();
							cuto_listView.onLoadBottomEnd();
						} else {
							if(been.size()>0){
								
									adapter.addData(been);
									adapter.notifyDataSetChanged();
									if (adapter.getCount() > 0)
										message_id=((MessageBeen)(adapter.getItem(adapter.getCount()-1)))._id;
								
							}else {
								cuto_listView.onLoadBottomEnd();
								Toast.makeText(getActivity(), "已经到底部了", Toast.LENGTH_SHORT).show();
							}
						}
					}
				}
			}
			@Override
					public void onStart() {
						// TODO Auto-generated method stub
						super.onStart();
						loadingDialog.show();
					}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				requestUrl, requestCallBack);
		HttpRequest.send(params);
	}
}
